package dg;

import java.util.*;

public class Main {
    public static final int SUPPORT = 3;

    public static void main(String[] args) {
        Set<String> itemCheck1 = new HashSet<>();
        itemCheck1.add("M");
        itemCheck1.add("O");
        itemCheck1.add("N");
        itemCheck1.add("K");
        itemCheck1.add("E");
        itemCheck1.add("Y");

        Set<String> itemCheck2 = new HashSet<>();
        itemCheck2.add("D");
        itemCheck2.add("O");
        itemCheck2.add("N");
        itemCheck2.add("K");
        itemCheck2.add("E");
        itemCheck2.add("Y");

        Set<String> itemCheck3 = new HashSet<>();
        itemCheck3.add("M");
        itemCheck3.add("A");
        itemCheck3.add("K");
        itemCheck3.add("E");

        Set<String> itemCheck4 = new HashSet<>();
        itemCheck4.add("M");
        itemCheck4.add("U");
        itemCheck4.add("C");
        itemCheck4.add("K");
        itemCheck4.add("Y");

        Set<String> itemCheck5 = new HashSet<>();
        itemCheck5.add("C");
        itemCheck5.add("O");
        itemCheck5.add("O");
        itemCheck5.add("K");
        itemCheck5.add("I");
        itemCheck5.add("E");

        List<List<String>> itemCheckList = new ArrayList<>();
        itemCheckList.add(new ArrayList<>(itemCheck1));
        itemCheckList.add(new ArrayList<>(itemCheck2));
        itemCheckList.add(new ArrayList<>(itemCheck3));
        itemCheckList.add(new ArrayList<>(itemCheck4));
        itemCheckList.add(new ArrayList<>(itemCheck5));

        Main main = new Main();
        Map<String, Integer> L1Map = main.L1(itemCheckList);
        Map<String, Integer> L2Map = main.L2(L1Map, itemCheckList);
        Map<String, Integer> L3Map = main.L3(L2Map, itemCheckList);

        printSetMap(L1Map, "1 项集");
        printSetMap(L2Map, "2 项集");
        printSetMap(L3Map, "3 项集");
    }

    private static void printSetMap(Map<String, Integer> setMap, String s) {
        System.out.println("===========================" + s + "====================================");
        Set<Map.Entry<String, Integer>> entries = setMap.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            System.out.println(entry.getKey() + " = " + entry.getValue());
        }
        System.out.println("===============================================================");
        System.out.println();
    }

    private Map<String, Integer> L3(Map<String, Integer> l2Map, List<List<String>> itemCheckList) {
        Set<String> L2KeySet = l2Map.keySet();
        List<String> L2KeyList = new ArrayList(L2KeySet);
        List<String> L3KeyList = new ArrayList<>();
        Map<String, Integer> L3Map = new HashMap<>();

        int len = L2KeyList.size();
        for (int i = 0; i < len - 1; i++) {
            for (int j = i + 1; j < len; j++) {
                String s1 = L2KeyList.get(i);
                String s2 = L2KeyList.get(j);
                if (s2.indexOf(s1.charAt(0) + "") != -1 || s2.indexOf(s1.charAt(1) + "") != -1) {
                    String l3SetItem = getL3SetItem(s1, s2);
                    L3KeyList.add(l3SetItem);
                }
            }
        }

        for (String l3Item : L3KeyList) {
            int count = 0;
            for (List<String> itemCheck : itemCheckList) {
                if (itemCheck.contains(l3Item.charAt(0) + "") && itemCheck.contains(l3Item.charAt(1) + "") && itemCheck.contains(l3Item.charAt(2) + "")) {
                    count++;
                }
            }
            if (count >= SUPPORT) {
                L3Map.put(l3Item, count);
            }
        }
        return L3Map;

    }

    private String getL3SetItem(String s1, String s2) {
        Set<Character> set = new HashSet<>();
        int len1 = s1.length();
        int len2 = s2.length();

        for (int i = 0; i < len1; i++) {
            set.add(s1.charAt(i));
        }
        for (int i = 0; i < len2; i++) {
            set.add(s2.charAt(i));
        }

        String str = "";
        for (Character s : set) {
            str += s;
        }
        return str;
    }

    private Map<String, Integer> L2(Map<String, Integer> l1Map, List<List<String>> itemCheckList) {
        Set<String> L1KeySet = l1Map.keySet();
        List<String> L1KeyList = new ArrayList(L1KeySet);
        List<String> L2KeyList = new ArrayList<>();
        Map<String, Integer> L2Map = new HashMap<>();
        int len = L1KeyList.size();
        for (int i = 0; i < len - 1; i++) {
            for (int j = i + 1; j < len; j++) {
                String s1 = L1KeyList.get(i);
                String s2 = L1KeyList.get(j);
                L2KeyList.add(s1 + s2);
            }
        }

        for (String l2Item : L2KeyList) {
            int count = 0;
            for (List<String> itemCheck : itemCheckList) {
                if (itemCheck.contains(l2Item.charAt(0) + "") && itemCheck.contains(l2Item.charAt(1) + "")) {
                    count++;
                }
            }
            if (count >= SUPPORT) {
                L2Map.put(l2Item, count);
            }
        }
        return L2Map;
    }

    public Map<String, Integer> L1(List<List<String>> itemCheckList) {
        Map<String, Integer> L1Map = new HashMap<>();
        Set<String> itemSet = new HashSet<>();
        for (List<String> itemCheck : itemCheckList) {
            for (String item : itemCheck) {
                itemSet.add(item);
            }
        }

        for (String item : itemSet) {
            int count = 0;
            for (List<String> itemCheck : itemCheckList) {
                if (itemCheck.contains(item)) {
                    count++;
                }
            }

            if (count >= SUPPORT) {
                L1Map.put(item, count);
            }
        }
        return L1Map;
    }
}
